package com.chia.multienty.core.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chia.multienty.core.domain.dto.AppSettingDTO;
import com.chia.multienty.core.domain.enums.StatusEnum;
import com.chia.multienty.core.mapper.AppSettingMapper;
import com.chia.multienty.core.mybatis.MTLambdaWrapper;
import com.chia.multienty.core.mybatis.service.impl.KutaBaseServiceImpl;
import com.chia.multienty.core.parameter.wechat.*;
import com.chia.multienty.core.pojo.AppSetting;
import com.chia.multienty.core.service.AppSettingService;
import com.chia.multienty.core.tools.IdWorkerProvider;
import com.chia.multienty.core.tools.MultientyContext;
import com.chia.multienty.core.util.ListUtil;
import com.github.yulichang.toolkit.MPJWrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 应用设置 服务实现类
 * </p>
 *
 * @author Multienty Auto Generator
 * @since 2024-06-17
 */
@Service
@RequiredArgsConstructor
public class AppSettingServiceImpl extends KutaBaseServiceImpl<AppSettingMapper, AppSetting> implements AppSettingService {


    @Override
    public AppSettingDTO getDetail(AppSettingDetailGetParameter parameter) {
        return selectJoinOne(AppSettingDTO.class,
                        MPJWrappers.<AppSetting>lambdaJoin().eq(AppSetting::getSettingId, parameter.getSettingId()));
    }

    @Override
    public void update(AppSettingUpdateParameter parameter) {
        AppSetting appSetting = new AppSetting();
        BeanUtils.copyProperties(parameter, appSetting);
        updateByIdTE(appSetting);
    }

    @Override
    public void delete(AppSettingDeleteParameter parameter) {
        removeByIdTE(parameter.getSettingId());
    }

    @Override
    public IPage<AppSettingDTO> getPage(AppSettingPageGetParameter parameter) {
        return selectJoinListPage(parameter.getPageObj(), AppSettingDTO.class,
                new MTLambdaWrapper<AppSetting>()
                        .solveGenericParameters(parameter)
                        .eq(AppSetting::getProgramId, parameter.getProgramId())
                        .in(!ListUtil.isEmpty(parameter.getSettingIds()),
                                AppSetting::getSettingId,
                                parameter.getSettingIds())
        );
    }

    @Override
    public void save(AppSettingSaveParameter parameter) {
        AppSetting appSetting = new AppSetting();
        BeanUtils.copyProperties(parameter, appSetting);
        appSetting.setSettingId(IdWorkerProvider.next());

        saveTE(appSetting);
        parameter.setSettingId(appSetting.getSettingId());
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void sync(AppSettingSyncParameter parameter) {
        if (ListUtil.isEmpty(parameter.getList())) {
            throw new IllegalArgumentException();
        }
        List<AppSetting> updateList = parameter.getList().stream().filter(p -> p.getSettingId() != null).collect(Collectors.toList());
        if (ListUtil.isNotEmpty(updateList)) {
            updateList.forEach(entity -> {
                entity.setTenantId(MultientyContext.getTenant().getTenantId());
            });
            updateBatchByIdAndSharding(updateList);
        }
        List<AppSetting> saveList = parameter.getList().stream().filter(p -> p.getSettingId() == null).collect(Collectors.toList());
        if (ListUtil.isNotEmpty(saveList)) {
            saveList.forEach(entity -> {
                entity.setSettingId(IdWorkerProvider.next());
                entity.setStatus(StatusEnum.NORMAL.getCode());
                entity.setTenantId(MultientyContext.getTenant().getTenantId());
            });
            saveBatch(saveList);
        }

    }


    @Override
    public void enable(AppSettingEnableParameter parameter) {
        AppSetting appSetting = new AppSetting();
        BeanUtils.copyProperties(parameter, appSetting);
        appSetting.setStatus(StatusEnum.NORMAL.getCode());
        updateByIdTE(appSetting);
    }

    @Override
    public void disable(AppSettingDisableParameter parameter) {
        AppSetting appSetting = new AppSetting();
        BeanUtils.copyProperties(parameter, appSetting);
        appSetting.setStatus(StatusEnum.DISABLED.getCode());
        updateByIdTE(appSetting);
    }
}
